package com.study.ju.model.res;

import com.study.ju.enums.RespCode;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import java.io.Serializable;

/**
 * {
 *  "head":{"code":"200","msg":"返回正确"},
 *  "body":{"xxx":"xxxx","xxx":"xxxx"}
 *  }
 * 
 * 返回数据格式
 * @author qiuzhongtian
 * @createtime 2013-11-11 下午01:13:08
 */
public class ActionResponse<E> implements Serializable {
    /**
     * 
     */
    private static final long serialVersionUID = 7167009484395159548L;

    /** 结果头部信息 */
    private Head              head             = new Head();
    /** 结果实体部分 */
    private Body              body             = new Body();

    public ActionResponse() {
        super();
    }

    public static <E> ActionResponse<E> of() {
        return new ActionResponse<E>(RespCode._C200);
    }

    public static <E> ActionResponse<E> of(RespCode respCode) {
        return new ActionResponse<E>(respCode);
    }

    public static <E> ActionResponse<E> of(RespCode respCode, E result) {
        return new ActionResponse<E>(respCode, result);
    }

    public static <E> ActionResponse<E> of(E result) {
        return new ActionResponse<E>(RespCode._C200, result);
    }

    public static <E> ActionResponse<E> of(RespCode respCode, String msg) {
        return new ActionResponse<E>(respCode, msg);
    }

    public ActionResponse(RespCode respCode) {
        super();
        this.head.setCode(respCode.code());
        this.head.setMsg(respCode.value());
    }

    public ActionResponse(RespCode respCode, E result) {
        super();
        this.head.setCode(respCode.code());
        this.head.setMsg(respCode.value());

        //        if(result instanceof DataResult){//暂时处理，消除返回信息的复杂嵌套
        //        	this.body = ((DataResult) result).getData();
        //        }else{
        //        	this.body = result;
        //        }
        this.body.setResult(result);
    }

    public ActionResponse(RespCode respCode, String msg) {
        super();
        this.head.setCode(respCode.code());
        this.head.setMsg(msg);
    }

    public void setStatus(RespCode respCode) {
        this.head.setCode(respCode.code());
        this.head.setMsg(respCode.value());
    }

    public Head getHead() {
        return head;
    }

    public E getBody() {
        return body.getResult();
    }

    public void setBody(E body) {
        this.body.setResult(body);
    }

    public void setHead(Head head) {
        this.head = head;
    }

    /**
     * 接口返回状态.
     * 前端根据头部信息来判断当前的结果状态.
     * @author qiuzhongtian
     * @createtime 2013-11-11 下午03:11:23
     */
    public class Head {
        /**
         * 结果状态代码
         * @see RespCode
         */
        private String code;

        private String msg;

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }

        public String getMsg() {
            return msg;
        }

        public void setMsg(String msg) {
            this.msg = msg;
        }

        public String getStime() {
            return String.valueOf(System.currentTimeMillis());
        }

    }

    /**
     * 接口返回的业务数据. 页面显示的数据,或者是错误信息的详细.
     */
    private class Body {
        private E result;

        public E getResult() {
            return result;
        }

        public void setResult(E result) {
            this.result = result;
        }

        @Override
        public String toString() {
            return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
        }
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

}
